package com.msb.strategy.util;

import cn.hutool.dfa.WordTree;
import com.msb.common.constart.CacheConstant;
import com.msb.common.util.SpringUtil;
import com.msb.strategy.feign.BeaconCacheClient;

import java.util.List;
import java.util.Set;

/**
 * @Author: lmf
 * @Create: 2025/3/13 9:08
 * @module
 */
public class HutoolDFAUtil {
    private static WordTree wordTree = new WordTree();
    /**
     * 初始化敏感词树
     */
    static {
        // 获取Spring容器中的cacheClient
        BeaconCacheClient cacheClient = SpringUtil.getBeanByClass(BeaconCacheClient.class);
        // 获取存储在Redis中的全部敏感词
        Set<String> dirtyWords = cacheClient.sMembersString(CacheConstant.DIRTYWORD);
        // 调用WordTree的add方法，将dfaMap的敏感词树构建
        wordTree.addWords(dirtyWords);
    }

    /**
     * 验证敏感词
     * @return  敏感词
     */
    public static List<String> getDirtyWords(String text){
        return wordTree.matchAll(text);
    }
}
